import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-12
 * Time: 19:39
 * author:JdbcDelete
 */
public class JdbcUptate {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_104?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.建立连接
        Connection connection = dataSource.getConnection();

        //3. 用户输入 修改的内容
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的 ID :");
        int id = scanner.nextInt();
        System.out.println("请输入要修改的 姓名 :");
        String name = scanner.next();

        //4.操作数据库
        //构造操作数据库的语句
        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);
        System.out.println(statement);

        //5.执行sql
        int ret = statement.executeUpdate();

        //6.释放资源
        statement.close();
        connection.close();

    }
}
